﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Text;
using JQuery.Net.Modules;

namespace JQuery.Net
{
    public class ResourceManager
        : IEnumerable<Resource>
    {
        #region "constructors"
        internal ResourceManager(bool optimize)
            : base()
        {
            resources = new List<Resource>();
            optimize_scripts = optimize;
        }
        #endregion

        #region "fields"
        private List<Resource> resources = null;
        private bool optimize_scripts = true;
        private bool minimized;
        #endregion

        #region "properties"
        /// <summary>
        /// Get a reference to a list of available resources
        /// </summary>
        public Resources Resources
        {
            get { return JQNModule.Resources; }
        }
        public bool IsOptimized { get { return optimize_scripts; } set { optimize_scripts = value; } }
        public bool IsMinimized { get { return minimized; } set { minimized = value; } }
        public IEnumerable<Resource> CSS
        {
            get
            {
                return this.AsQueryable()
                    .Where((X) =>
                        X.ContentType == Resource.ContentTypes.css);
            }
        }
        #endregion

        #region "public methods"
        /// <summary>
        /// Register a JQuery Script include 
        /// </summary>
        /// <param name="key">script key</param>
        /// <param name="url">script url</param>
        public void RegisterJQNInclude(String key, String url)
        {
            RegisterScriptInclude(null, key, url, false);
        }
        /// <summary>
        /// Register a JQuery Script Resource to be included on the page.
        /// </summary>
        /// <param name="type">qualified assembly type name of resource</param>
        /// <param name="resource">script resource uri</param>
        public void RegisterJQNInclude(Type type, String resource)
        {
            RegisterScriptInclude(type, resource, null, false);
        }

        public bool IsResourceRegistered(String key)
        {
            return resources.Any((X) => X.Key == key);
        }

        #region "private methods"
        internal void RegisterScriptInclude(Type type, String key, String url, Boolean exclude)
        {
            if (key.IsNullOrEmpty())
            {   /// the key parameter is the only one that can not be null
                throw new ArgumentNullException("key");
            }

            Resource resource = new Resource(key, url, type, Resource.ContentTypes.javascript)
            {
                ExcludeResource = exclude
            };
            /// check this pages resource list
            if (IsResourceRegistered(key) == false)
            {
                resources.Add(resource);
            }
            /// we need to check the module resource list
            if (JQNModule.Resources.IsResourceRegistered(key) == false)
            {
                JQNModule.Resources.Add(resource);
            }
        }
        #endregion
        #endregion


        public void Add(Resource item)
        {
            if (!resources.Any((X) => X.Key == item.Key))
            {
                /// we need to make sure this resource is available outside the instance of the current page request
                if (!Resources.IsResourceCheckSumRegistered(item.CheckSum))
                    Resources.Add(item);
                /// add the resource to the resource list be put together for this page request
                resources.Add(item);
            }
        }

        #region IEnumerable<Resource> Members

        public IEnumerator<Resource> GetEnumerator()
        {
            return resources.GetEnumerator();
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return resources.GetEnumerator();
        }

        #endregion
    }
}
